#region Summary

// 
// Owner : JackieHan 
// 
// Solution : LearnSolution 
// 
// Project : JackieHan.ParallelPragrammingWithDotNet4 
// 
// File : Task.cs 
// 
// CopyRight : Copyright © 2009-2010 XunGu Corporation, All Rights Reserved 
// 
// Summary : 并行编程核心类Task 
// 
// History : 
//			2010-05-26 14:27:17	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;

#endregion

namespace JackieHan.ParallelPragrammingWithDotNet4
{
    /// <summary> 
    /// 并行编程核心类Task  
    /// </summary> 
    [TestFixture]
    public class TaskClass
    {

        [Test]
        public void TestDefaultTask()
        {
            Task task1 = new Task(new Action(PrintMessage));
            Task task2 = new Task(delegate { PrintMessage(); });
            Task task3 = new Task(() => PrintMessage());
            Task task4 = new Task(() => { PrintMessage(); });

            task1.Start();
            task2.Start();
            task3.Start();
            task4.Start();
        }

        public static void PrintMessage()
        {
            Console.WriteLine("Hello ParallelPragramming!");
        }

        [Test]
        public void TestParameter()
        {
            string str = "JackieHan";
            Task task = new Task(obj => Console.WriteLine("Message:{0}", obj), str);
            task.Start();
        }

        [Test]
        public void TestGetResult()
        {
            Task<int> task = new Task<int>(() =>
            {
                int sum = 0; 
                for (int i = 1; i <= 100; i++)
                {
                    sum += i;
                }
                return sum;
            });
            task.Start();
            Console.WriteLine("TaskId:{0}", task.Id);
            Console.WriteLine("Result:{0}", task.Result);
            Assert.AreEqual(5050, task.Result);
        }

        [Test]
        public void TestCancelTask()
        {
            CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;

            Task task = new Task(() =>
                                     {
                                         for (int i = 0; i < int.MaxValue; i++)
                                         {
                                             if (token.IsCancellationRequested)
                                             {
                                                 Console.WriteLine("Task cancel detected");
                                                 throw new OperationCanceledException(token);
                                             }
                                             else
                                             {
                                                 Console.WriteLine("Int value {0}", i);
                                             }
                                         }
                                     },token);
            task.Start();
            
            Thread.Sleep(10000);
            tokenSource.Cancel();
        }

        [Test]
        public void TestCancelTaskWithDeledge()
        {
            CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;

            Task task = new Task(() =>
                                     {
                                         for (int i = 0; i < int.MaxValue; i++)
                                         {
                                             if (token.IsCancellationRequested)
                                             {
                                                 Console.WriteLine("Task cancel detected");
                                                 throw new OperationCanceledException(token);
                                             }
                                             else
                                             {
                                                 Console.WriteLine("Int Value {0}", i);
                                             }
                                         }
                                     }, token);

            token.Register(() => Console.WriteLine(">>>>>Delegate Invokend \n"));

            task.Start();

            Thread.Sleep(1000);

            Console.WriteLine("Cancelling Task");
            tokenSource.Cancel();
        }


    }


}
